package com.example.near.controller;

import com.example.near.entity.R;
import com.example.near.entity.ServiceStation;
import com.example.near.service.ServiceStationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/service")
public class ServiceController {
    @Autowired
    private ServiceStationService risGeoService;

    /**
     * 将数据库经纬度插入缓存
     * @param id
     * @return
     */
    @PostMapping("/test")
    public Long test(Integer id) {
        List<ServiceStation> list = risGeoService.list(id);
        return risGeoService.saveServiceStationToRedis(list);
    }

    /**
     * 根据地名查询经纬度
     * @param serviceStations
     * @return
     */
    @GetMapping("/location")
    public R getLocation(@RequestBody String[] serviceStations){
        return R.ok(risGeoService.getServiceStationPos(serviceStations));
    }

    /**
     * 获取两个油站之间的距离
     * @param serviceStation1 第一个油站
     * @param serviceStation2 第二个油站
     * @param metric {@link Metric} 单位信息, 可以是 null
     * @return {@link Distance}
     * */
    @GetMapping("/juli")
    public R getTwoServiceStationDistance(String serviceStation1, String serviceStation2, String metric){
        Metrics metrics;
        if (metric != null){
            metrics = Metrics.KILOMETERS;
        }else {
            metrics = null;
        }
        return R.ok(risGeoService.getTwoServiceStationDistance(serviceStation1,serviceStation2,metrics));
    }

    /**
     * 根据给定地理位置坐标(经纬度)获取指定范围内的地理位置集合
     * @param l1 经度
     * @param l2 纬度
     * @param r  半径
     * @return
     */
    @GetMapping("/near")
    public R near(Double l1, Double l2,int r){
        Point center = new Point(l1,l2);
        Distance radius = new Distance(r, Metrics.KILOMETERS);
        Circle within = new Circle(center, radius);
        RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().sortAscending();//includeDistance().limit(2) 可限制查询结果数量
        return R.ok(risGeoService.getPointRadius(within,args),"查询成功");
    }

    /**
     * 根据给定地理位置获取指定范围内的地理位置集合
     * @param name 名称
     * @param r
     * @return
     */
    @GetMapping("/nearByName")
    public R near(String name,int r){
        Distance radius = new Distance(r, Metrics.KILOMETERS);
        RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().sortAscending();//includeDistance().limit(2) 可限制查询结果数量
        return R.ok(risGeoService.getMemberRadius(name,radius,args));
    }

    /**
     * 获取某个地理位置的 geohash 值
     * @param serviceStations 给定油站 key
     * @return city geohashs
     */
    @GetMapping("/getGeoHash")
   public R getServiceStationGeoHash(@RequestBody String[] serviceStations){
        return R.ok(risGeoService.getServiceStationGeoHash(serviceStations));
    }
}
